<template>
  <view class="tab">
    <view
      class="tabItem"
      v-for="item of items"
      :key="item.id"
      :class="[getClass(item.id)]"
      @click="change(item.id)"
    >
      <view class="tabBox">
        {{ item.name }}
        <view class="icon"></view>
      </view>
    </view>

    <view class="tabItem">
      <slot></slot>
    </view>
  </view>
</template>

<script>
export default {
  props: {
    index: {
      type: Number,
      default: 0,
    },

    items: {
      type: Array,
      default() {
        return [];
      },
    },
  },

  data() {
    return {};
  },

  methods: {
    change(id) {
      if (id === this.index) {
        return;
      }

      this.$emit("change", id);
    },
  },

  computed: {
    getClass() {
      return (id) => {
        return id === this.index ? "on" : "";
      };
    },
  },
};
</script>

<style lang="less" scoped>
.tab {
  position: relative;
  z-index: 3;
  display: flex;
  align-items: center;
  padding: 0;
  background: #fff;
}

.tabItem {
  position: relative;
  width: 25%;
  text-align: center;
  height: 100rpx;
  line-height: 100rpx;

  .icon {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 16rpx;
    height: 6rpx;
    background: #f0250e;
    display: none;
  }

  &.on {
    .text {
      font-size: 32rpx;
      color: #262626;
    }

    .icon {
      display: block;
    }
  }
}

.tabBox {
  position: relative;
  display: inline-block;
}

.text {
  position: relative;
  display: inline-block;
  font-size: 28rpx;
  color: #8c8c8c;
  transition: 0.3s all;
}
</style>
